package cn.walket.cloud.user.db;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.proxy.jdbc.StatementProxy;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author walket
 * @create 2020/12/9 11:38 上午
 */
@Slf4j
public class SlowSqlStatFilter extends StatFilter {


  public SlowSqlStatFilter(Long slowSqlMillis,
      boolean logSlowSql, boolean mergeSql) {
    super();
    super.setMergeSql(mergeSql);
    super.setSlowSqlMillis(slowSqlMillis);
    super.setLogSlowSql(logSlowSql);
  }

  @Override
  protected void handleSlowSql(StatementProxy statementProxy) {
    super.handleSlowSql(statementProxy);
    String lastExecSql = statementProxy.getLastExecuteSql();
    final long nowNano = System.nanoTime();
    final long nanos = nowNano - statementProxy.getLastExecuteStartNano();
    long millis = nanos / (1000 * 1000);
    if (millis >= 20) {
      log.warn("[SLOW] {}ms,{}", millis,lastExecSql);
    }
//    if (millis >= slowSqlMillis) {
//      String curBizNo = LogUtils.getCurBizNo();
//      SlowSQLAlarmMsg msg = new SlowSQLAlarmMsg(curBizNo, millis, lastExecSql.replace("\n", ""));
//      alarmService.alaram(msg);
//    }
  }
}